package planbox::Controller::Notetype;
use Moose;
use namespace::autoclean;

use wt::ncrud4mdb;
use wt::NtDao;
use Data::Dumper;
use wt;

BEGIN {extends 'Catalyst::Controller::REST'; }

sub index :Path :Args(0) {
    my ($self, $c) = @_;

    &notetype_POST if $c->req->data;

    my $params = $c->req->params;
    my $start = $$params{start};
    my $limit = $$params{limit};

    $start = 0 if !$start; 
    $limit = 10 if !$limit;

    my $ntdao = new wt::NtDao( "db"=>"testdb");
    my @root_nts = $ntdao->getAll($start, $limit);

    my $ntcount = $ntdao->count;
    $c->log->debug("count ". $ntcount);
    my $ret = {
        "success" => 1,
        "total" => $ntcount, 
        "rows" => [ @root_nts ]
    };

    $self->status_ok($c, entity => $ret);
}

=head2 rootnts

=cut

sub rootnts :Path :Local :ActionClass('REST') { }
sub rootnts_GET { 
    my ($self, $c) = @_;

    my $ntdao = new wt::NtDao( "db"=>"testdb");
    my @root_nts = $ntdao->getRoots();
    $self->status_ok($c, entity => \@root_nts);
}

sub tree :Path('tree') :Args(0) {
    my ($self, $c, $id) = @_;

    my $params = $c->req->params;
    my $id = $$params{node};
    
    my $ntdao = new wt::NtDao( "db"=>"testdb");
    my @nts = $ntdao->getSubs( $id );
    $c->log->debug("nts for [$id] " );

    $self->status_ok($c, entity => \@nts);
}
=head2 nts

=cut

sub nts :Path :Local :ActionClass('REST') :Args(1) {
    my ($self, $c, $id) = @_;
    $c->stash(id => $id);
}
sub nts_GET { 
    my ($self, $c) = @_;
    #my $id = $c->stash->{id};

    my $params = $c->req->params;
    my $id = $$params{node};
    
    my $ntdao = new wt::NtDao( "db"=>"testdb");
    my @nts = $ntdao->getSubs( $id );
    $c->log->debug("nts for [$id] ". Dumper(\@nts));

    $self->status_ok($c, entity => \@nts);
}

=head2 notetype

=cut

sub notetype :Path :Local :ActionClass('REST') :Args(1) {
    my ($self, $c, $id) = @_;
    $c->stash(id => $id);

    $c->log->debug("in notetype for [$id] ". ref $id);
}
sub notetype_GET { 
    my ($self, $c) = @_;
    my $id = $c->stash->{id};
    
    my $ntdao = new wt::NtDao( "db"=>"testdb");
    my $nt = $ntdao->get( $id );

    $c->log->debug("in notetype_GET for [$id] ". ref $id);
    $self->status_ok($c, entity => $nt);
}
sub notetype_POST { 
    my ($self, $c) = @_;
 
    my $nt = $c->req->data;

    my $ntdao = new wt::NtDao( "db"=>"testdb");
    my $saveid = $ntdao->save( $nt );

    $c->log->debug("in notetype_POST for [$nt] ". ref $nt);
    $c->log->debug("$saveid");

    $self->status_ok($c, entity => $nt);
}

*notetype_PUT = *notetype_POST;

sub notetype_DELETE { 
    my ($self, $c) = @_;
    my $id = $c->stash->{id};
    my $nt = $c->req->data;

    my $ntdao = new wt::NtDao( "db"=>"testdb");
    $ntdao->rm( $id );

    $c->log->debug("in notetype_DELETE for [$id] ". ref $id);

    $self->status_ok($c, entity => $nt);
}

=head1 AUTHOR

rot,,,

=head1 LICENSE

This library is free software. You can redistribute it and/or modify
it under the same terms as Perl itself.

=cut

__PACKAGE__->config(default => 'application/json');

__PACKAGE__->meta->make_immutable;

1;
